﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    [DefaultEvent("TextChanged")]
    [DefaultBindingProperty("Text")]
    public class WenLineTextBox : WenControl
    {
        public WenLineTextBox() : base()
        {
            textBox = new TextBox()
            {
                BorderStyle = BorderStyle.None,
                Anchor = AnchorStyles.Left | AnchorStyles.Right,
            };

            //鼠标移动
            textBox.MouseLeave += TextBox_MouseLeave;
            textBox.MouseEnter += TextBox_MouseEnter;
            this.MouseLeave += TextBox_MouseLeave;
            this.MouseEnter += TextBox_MouseEnter;

            this.MouseMove += WenLineTextBox_MouseMove;

            textBox.LostFocus += TextBox_LostFocus;
            textBox.GotFocus += TextBox_GotFocus;

            //创建消息 NativeWindow
            textBox.HandleCreated += TextBox_HandleCreated;
            textBox.HandleDestroyed += TextBox_HandleDestroyed;

            //鼠标点击事件
            this.MouseClick += WenLineTextBox_MouseClick;

            this.SizeChanged += WenLineTextBox_SizeChanged;
            this.Controls.Add(textBox);

            //初始化颜色
            BackColor = Color.FromArgb(37, 37, 38);
            BorderColor = Color.FromArgb(63, 63, 70);
            HotColor = Color.FromArgb(0, 122, 204);
            ForeColor = Color.FromArgb(241, 241, 241);
            WateTextColor = Color.FromArgb(153, 153, 153);

            PasswordChar = '\0';
        }

        #region 私有属性
        private Color hotColor;
        private Color borderColor;
        private bool isMouseOver;
        private string wateText;
        private Color wateTextColor;
        private bool buttonImageMouseOver;
        private HeaderNativeWindow headerNativeWindow;
        private Image buttonImage;

        private TextBox textBox { get; set; }
        private bool IsMouseOver { get => isMouseOver; set { isMouseOver = value; this.Invalidate(); } }

        private bool ButtonImageMouseOver { get => buttonImageMouseOver; set { buttonImageMouseOver = value; this.Invalidate(); } }
        #endregion

        #region 公用属性

        [Category("Wen颜色"), Description("背景颜色")]
        [DefaultValue(typeof(Color), "37, 37, 38")]
        public new Color BackColor
        {
            get => base.BackColor;
            set
            {
                value = value == Color.Transparent ? Color.FromArgb(37, 37, 38) : value;
                base.BackColor = value;
                textBox.BackColor = value;
            }
        }

        [Category("Wen颜色"), Description("前景颜色")]
        [DefaultValue(typeof(Color), "241, 241, 241")]
        public new Color ForeColor
        {
            get => base.ForeColor;
            set
            {
                value = value == Color.Transparent ? Color.FromArgb(37, 37, 38) : value;
                base.ForeColor = value;
                textBox.ForeColor = value;
            }
        }

        [Category("Wen颜色"), Description("热点颜色或者鼠标在控件颜色")]
        [DefaultValue(typeof(Color), "0,122,204")]
        public Color HotColor { get => this.hotColor; set { this.hotColor = value; this.Invalidate(); } }

        [Category("Wen颜色"), Description("获得或设置控件的边框颜色")]
        [DefaultValue(typeof(Color), "63,63,70")]
        public Color BorderColor { get => this.borderColor; set { this.borderColor = value; this.Invalidate(); } }

        [Category("Wen颜色"), Description("提示字符颜色")]
        [DefaultValue(typeof(Color), "153,153,153")]
        public Color WateTextColor { get => this.wateTextColor; set { this.wateTextColor = value; this.Invalidate(); } }


        [Category("Wen设计"), Description("当文本类容为空的时候提示内容")]
        [DefaultValue(null)]
        public string WateText { get => wateText; set { wateText = value; this.Invalidate(); } }

        [Category("Wen设计"), Description("尾部按钮图标")]
        [DefaultValue(null)]
        public Image ButtonImage
        {
            get => buttonImage;
            set
            {
                buttonImage = value;
                WenLineTextBox_SizeChanged(null, EventArgs.Empty);
                this.Invalidate();
            }
        }
        #endregion

        #region TextBox 属性

        [Category("Wen设计"), Description("文本内容")]
        [DefaultValue(null)]
        [Localizable(true)]
        public new string Text { get => textBox.Text; set => textBox.Text = value; }

        [Category("Wen设计"), Description("可编辑状态")]
        [DefaultValue(false)]
        public bool ReadOnly { get => textBox.ReadOnly; set => textBox.ReadOnly = value; }

        [Category("Wen设计"), Description("文本格式")]
        public new Font Font { get => textBox.Font; set { textBox.Font = value; base.Font = value; } }

        [Category("Wen设计"), Description("密码掩盖字符")]
        [DefaultValue('\0')]
        public char PasswordChar { get => textBox.PasswordChar; set => textBox.PasswordChar = value; }
        #endregion

        #region 委托

        [Category("Wen"), Description("图标按钮点击事件")]
        public event EventHandler ButtonClick;

        #endregion

        #region 创建消息 TextBox 消息拦截重绘

        private void TextBox_HandleDestroyed(object sender, EventArgs e)
        {
            if (headerNativeWindow != null)
            {
                headerNativeWindow = null;
            }
        }

        private void TextBox_HandleCreated(object sender, EventArgs e)
        {
            if (headerNativeWindow == null)
            {
                headerNativeWindow = new HeaderNativeWindow(this);
            }
        }

        private class HeaderNativeWindow : NativeWindow
        {
            private readonly WenLineTextBox owner;
            readonly IntPtr header;
            public HeaderNativeWindow(WenLineTextBox owner)
            {
                this.owner = owner;
                header = owner.textBox.Handle;
                base.AssignHandle(header);
            }
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                if (m.Msg == 0xf || m.Msg == 0x133)
                {
                    using Graphics g = Graphics.FromHwnd(m.HWnd).SetGDIHigh();
                    //当文本为空，提示文本不为空，水印TextBox
                    if (string.IsNullOrEmpty(owner.Text) && !string.IsNullOrEmpty(owner.WateText))
                    {
                        using Brush b = new SolidBrush(owner.WateTextColor);
                        g.DrawString(owner.WateText, owner.Font, b, new Rectangle(0, 0, owner.textBox.Width, owner.textBox.Height), ControlHelper.StringConters);
                    }
                }
            }
        }

        #endregion

        #region 鼠标移动 点击，获得焦点 失去事件 
        private void TextBox_LostFocus(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        private void TextBox_MouseEnter(object sender, EventArgs e)
        {
            IsMouseOver = true;
        }

        private void TextBox_MouseLeave(object sender, EventArgs e)
        {
            IsMouseOver = false;
            ButtonImageMouseOver = false;
        }
        private void TextBox_GotFocus(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        //鼠标点击

        private void WenLineTextBox_MouseClick(object sender, MouseEventArgs e)
        {
            Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
            if (recImageBack.Contains(e.Location) && e.Button == MouseButtons.Left && ButtonImage != null)
            {
                ButtonClick?.Invoke(this, e);
            }
        }

        //鼠标所在位置
        private void WenLineTextBox_MouseMove(object sender, MouseEventArgs e)
        {
            Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
            if (recImageBack.Contains(e.Location) && ButtonImage != null && !ButtonImageMouseOver)
            {
                ButtonImageMouseOver = true;
            }
            else if (!recImageBack.Contains(e.Location) && ButtonImageMouseOver)
            {
                ButtonImageMouseOver = false;
            }
        }
        #endregion

        private void WenLineTextBox_SizeChanged(object sender, EventArgs e)
        {
            textBox.Width = ButtonImage == null ? Width - 4 : Width - 16 - textBox.Height;
            textBox.Location = new Point(2, 5);
        }

        #region 重写事件 保持控件高度

        //保持控件宽度
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, textBox.Height + 8, specified);
        }

        #endregion

        #region 重绘 画外框

        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);
            Rectangle reck = new Rectangle(0, 0, Width - 1, Height - 1);

            if (IsMouseOver || textBox.Focused)
                g.DrawRectangle(new Pen(HotColor), reck);
            else
                g.DrawRectangle(new Pen(BorderColor), reck);

            if (ButtonImage != null)
            {
                Rectangle recImage = new Rectangle(this.Width - textBox.Height - 6, 4, textBox.Height, textBox.Height);
                if (ButtonImageMouseOver)
                {
                    Rectangle recImageBack = new Rectangle(this.Width - textBox.Height - 10, 0, textBox.Height + 10, this.Height);
                    g.FillRectangle(new SolidBrush(this.HotColor), recImageBack);
                }
                g.DrawImage(ButtonImage, recImage);
            }
        }
        #endregion
    }
}
